<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: liguohua@clcw.com.cn  Blog:http://blog.sina.com.cn/saverneo    |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\CarResource;

use AuctionCore\Account\Back\Employee;

/**
 * 车源列表
 * @author       Eric Lee
 */
class CarList extends \AuctionCore\System\ListBase {

    public function get($ev_page_num, $page) {
        
    }

    public function count() {
        
    }

    /**
     * 获取车源列表
     * @param integer $page
     * @param integer $size
     * @param array $paramer
     * @return multitype:array(list,total)
     */
    public function get_list($page = 1, $size = 10, $paramer) {
        $map = array();
        //条件 状态为待拍卖、待重拍的车
        //$map['c.car_status'] = array('in',array('4','7'));
        //车源状态
        if (in_array($paramer['car_status'], array('4', '7'))) {
            $map['c.car_status'] = array('eq', $paramer['car_status']);
        } elseif ($paramer['car_status'] == '2') {//拍单审核驳回
            $map['o.status'] = array('eq', '2');
        }
        //品牌id
        /*if ($paramer['brand_id']) {
            $map['c.brand_id'] = array('eq', $paramer['brand_id']);
        }*/
        //车系id
        /*if ($paramer['series_id']) {
            $map['c.series_id'] = array('eq', $paramer['series_id']);
        }*/
        //车型id
        /*if ($paramer['model_id']) {
            $map['c.model_id'] = array('eq', $paramer['model_id']);
        }*/
        //关键字(车辆编号，等级)
        if (!empty($paramer['keyword'])) {
            $where = array();
            $keyword = trim($paramer['keyword']);
            if (ctype_alpha($keyword) || ctype_digit($keyword)) {         //如果为纯数字或纯字符则模糊搜索
                $where['c.car_no'] = array('LIKE', '%' . $keyword . '%');
                $where['c.composite_state'] = array('LIKE', '%' . $keyword . '%');
            } else {
                $keyword_first = substr($keyword, 0, 1);
                $keyword_front = substr($keyword, 0, -1);
                $keyword_last = substr($keyword, -1);
                if (ctype_alpha($keyword_first)) {    //首字母为字符判定为车辆编号
                    $where['c.car_no'] = array('LIKE', '%' . $keyword . '%');
                }
                if (ctype_alpha($keyword_last)) {       //尾字符为字母判定为车辆等级
//                    $where['c.composite_state'] = array('eq', $keyword_front);
//                    $where['c.accident_level'] = array('eq', strtoupper($keyword_last));
                    $where['_string'] = "c.composite_state='" . $keyword_front . "' and c.accident_level='" . strtoupper($keyword_last) . "'";
                }
            }
//            $where['c.car_no'] = array('eq', urldecode($paramer['keyword']));
//            $where['c.composite_state'] = array('eq', urldecode($paramer['keyword']));
//            $where['c.accident_level'] = array('eq', urldecode($paramer['keyword']));
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        //$map['c.price_emp_id'] = array('neq',' ');
        $map['c.suggest_max_price'] = array('neq',' ');
        $map['c.suggest_min_price'] = array('neq',' ');
        $cols = "c.car_id,c.car_no,c.brand_id,c.series_id,c.model_id,c.car_status,c.reserve_price,c.suggest_min_price,c.suggest_max_price
                ,c.composite_state,c.accident_level,c.re_auction_type,c.re_auction_reason,o.status,o.remark_reject";
        $total = M('cars')->alias('c')->join("LEFT JOIN " . C('DB_PREFIX') . "order o ON c.car_id=o.car_id")
                        ->where("(c.car_status in('4','7') OR o.status='2')")->where($map)->count("DISTINCT c.car_id");
        $list = M('cars')->alias('c')->field($cols)->join("LEFT JOIN " . C('DB_PREFIX') . "order o ON c.car_id=o.car_id")
                        ->where("(c.car_status in('4','7') OR o.status='2')")->where($map)->limit($size)->page($page)->group('c.car_id')->order("car_id desc")->select();
       //echo M()->getLastSql();
        $cm = new \AuctionCore\CarResource\CarModel();
        //数据处理
        foreach ($list as $key => $val) {
            $list[$key] = $val;
            $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
            $series = $cm->get_model_by_id($val['series_id'], 'series');
            $model = $cm->get_model_by_id($val['model_id']);
            $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
            $list[$key]['reserve_price'] = format_money($val['reserve_price']);
            $list[$key]['suggest_min_price'] = format_money($val['suggest_min_price']);
            $list[$key]['suggest_max_price'] = format_money($val['suggest_max_price']);
            $list[$key]['car_status_cn'] = C('CAR_STATUS.' . $val['car_status']);
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 获取车辆查询列表
     * @param integer $page
     * @param integer $size
     * @param array $paramer
     * @return multitype:array(list,total)
     */
    public function get_car_list($page = 1, $size = 10, $params) {
        $map = array();
        $where = array();
        /**
         * 查询条件：
         * 0、检测时间 1、车辆状态 2、车辆所在地 3、车辆来源 4、车辆编号、级别、车牌号、车架号
         */
        if ($params['start_time']) {
            $map['co.check_date'] = array('egt', $params['start_time'] . ' 00:00:00');
        }
        if ($params['end_time']) {
            $map['co.check_date'] = array('elt', $params['end_time'] . ' 23:59:59');
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['co.check_date'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
        }

        if ($params['car_status'] > -1) {
            $map['c.car_status'] = array('eq', $params['car_status']);
        }
        if ($params['location_area']) {
            $map['co.location_area'] = array('eq', $params['location_area']);
        }
        if ($params['comefrom'] != '-2') {
            $map['co.comefrom'] = array('eq', $params['comefrom']);
        }
        if ($params['car_no']) {
            $car_no = trim($params['car_no']);
            $map['c.car_no'] = array('LIKE', '%' . $car_no . '%');
        }
        if ($params['car_level']) {
            $car_level = trim($params['car_level']);
            $car_level_front = substr($car_level, 0, -1);
            $car_level_last = substr($car_level, -1);
            if (ctype_digit($car_level)) {
                $map['c.composite_state'] = array('LIKE', '%' . $car_level . '%');
            }
            if (ctype_alpha($car_level)) {
                $map['c.accident_level'] = array('LIKE', '%' . $car_level . '%');
            }
            if ($car_level_front && ctype_alpha($car_level_last)) {
                $map['_string'] = "c.composite_state='" . $car_level_front . "' and c.accident_level='" . strtoupper($car_level_last) . "'";
            }
        }
        if ($params['car_plate_number']) {
            $car_plate_number = trim($params['car_plate_number']);
            $car_plate_number_first = mb_substr($car_plate_number, 0, 1, 'UTF8');
            $car_plate_number_last = mb_substr($car_plate_number, 1, 7, 'UTF8');
            $plate_prefix = array_search($car_plate_number_first, C('PLATE_PREFIX'));
            $plate_suffix = substr($car_plate_number, 1);
            $map['c.plate_prefix'] = array('eq', $plate_prefix);
            if ($car_plate_number_last) {
                $map['c.plate_suffix'] = array('LIKE', '%' . $car_plate_number_last . '%');
            }
        }
        if ($params['checker_id']) {
            $map['co.checker_id'] = array('eq', $params['checker_id']);
        }
//        if ($params['car_vin']) {
//            $map['c.vin'] = array('eq', $params['car_vin']);
//        }
        $cols = "c.car_id,c.car_no,c.brand_id,c.series_id,c.model_id,c.composite_state
                ,c.accident_level,c.car_status,c.plate_prefix,c.plate_suffix
                ,co.checker_id,co.comefrom,co.location_area,co.check_date";
        $order = "c.car_id DESC";
        $total = M('cars')->alias('c')
                        ->join(C('DB_PREFIX') . "car_owner as co ON c.`owner_id`=co.`owner_id`")
                        ->where($map)->count();
        $list = M('cars')->alias('c')
                ->join(C('DB_PREFIX') . "car_owner as co ON c.`owner_id`=co.`owner_id`")
                ->field($cols)
                ->where($map)
                ->limit($size)
                ->page($page)
                ->order($order)
                ->select();
//        echo M()->getLastSql();
        if (!empty($list)) {
            $addr = new \AuctionCore\Other\Address();
            $cm = new CarModel();
            foreach ($list as $key => $val) {
                $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
                $series = $cm->get_model_by_id($val['series_id'], 'series');
                $model = $cm->get_model_by_id($val['model_id']);
                $location_area = $addr->get_name_by_code($val['location_area']);
                $checker = Employee::user_info_id($val['checker_id']);
                $list[$key]['car_no'] = $val['car_no'];
                $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
                $list[$key]['car_level'] = $val['composite_state'] . $val['accident_level'];
                $list[$key]['car_plate_number'] = C('PLATE_PREFIX')[$val['plate_prefix']] . $val['plate_suffix'];
                $list[$key]['location_area'] = $location_area;
                $list[$key]['checker'] = $checker['real_name'];
                $list[$key]['check_date'] = date("Y年m月d日", strtotime($val['check_date']));
                $list[$key]['car_status'] = $val['car_status'];
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 获取car和车主列表
     * @param number $page
     * @param number $size
     * @param array $paramer
     * @return array:list,total
     */
    public function getCarAndOwnerList($page = 1, $size = 10, $params = array()) {
        $cols = "c.car_id,c.car_no,c.brand_id,c.series_id,c.model_id,c.car_status,c.suggest_min_price,c.suggest_max_price,c.price_emp_id,
            co.seller_name,co.phone,co.checker_id,e.real_name";
        $map = array();
        $order = '';
        if ($params['is_priced'] == 1) {   //未定价
            $map['c.suggest_min_price'] = array('elt', 0);
            $map['c.suggest_max_price'] = array('elt', 0);
            $order = "c.car_id ASC";
        } elseif ($params['is_priced'] == 2) { //已定价
            $map['c.suggest_min_price'] = array('gt', 0);
            $map['c.suggest_max_price'] = array('gt', 0);
            $order = "c.price_time DESC";
        }
        //评估师id
        if (!empty($params['checker_id'])){
            $map['co.checker_id'] = array('eq',$params['checker_id']);
        }
        //定价人
        if (!empty($params['price_id'])){
            $map['c.price_emp_id'] = array('eq',$params['price_id']);
        }
        //编号查询
        if (!empty($params['keyword'])){
            
            $map['c.car_no'] = array('LIKE', '%' . $params['keyword'] . '%');
        }
        $prefix = C('DB_PREFIX');
        $total = M("cars")->alias("c")
                        ->join("{$prefix}car_owner as co ON c.owner_id = co.owner_id")
                        ->join("{$prefix}employee as e ON e.emp_id=co.checker_id")
                        ->where($map)->count();
        $list = M("cars")->alias("c")->field($cols)
                ->join("{$prefix}car_owner as co ON c.owner_id = co.owner_id")
                ->join("{$prefix}employee as e ON e.emp_id=co.checker_id")
                ->where($map)->limit($size)->page($page)->order($order)->select();
       // echo M()->getLastSql();
    if (!empty($list)) {
            $cm = new \AuctionCore\CarResource\CarModel();
            foreach ($list as $key => $val) {
                $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
                $series = $cm->get_model_by_id($val['series_id'], 'series');
                $model = $cm->get_model_by_id($val['model_id']);
                $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
                if ($val['price_emp_id']) {
                    $Employee = Employee::user_info_id($val['price_emp_id']);
                    $price_person = $Employee['real_name'];
                } else {
                    $price_person = '--';
                }
                $list[$key]['price_person'] = $price_person;
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 获取审核列表
     * @param int $page
     * @param int $size
     * @param array $params
     * @return array $result
     * @author Gray An
     */
    public function check_list($page, $size, $params) {
        $page = (int) $page < 1 ? 1 : (int) $page;
        $size = (int) $size < 1 ? 10 : (int) $size;
        $order = "c.posttime DESC";
        $map = array();
        //车辆业务状态 1 待审核,2 审核失败,3 审核驳回,4 待拍卖（审核成功）
        if ($params['status'] == 1) {//待审核
            $map['c.car_status'] = array('eq', 1);
        } elseif ($params['status'] == 2) {//审核驳回
            $order = "c.audit_time DESC,c.posttime DESC";
            $map['c.car_status'] = array('eq', 3);
        } elseif ($params['status'] == 3) { //审核成功
            $order = "c.audit_time DESC,c.posttime DESC";
            $map['c.car_status'] = array('egt', 4);
        } elseif ($params['status'] == 4) { //审核失败
            $map['c.car_status'] = array('eq', 2);
            $order = "c.audit_time DESC,c.posttime DESC";
        } else {
            return array();
        }
        //上传时间
         if (!empty($params['start_time']) || !empty($params['end_time'])) {
            if (empty($params['start_time'])) {
                if($params['status'] == 1){
                    $map['c.posttime'] = array("elt", $params['end_time'].' 23:59:59');
                }else{
                    $map['c.audit_time'] = array("elt", $params['end_time'].' 23:59:59');
                }
            } elseif (empty($params['end_time'])) {
                if($params['status'] == 1){
                    $map['c.posttime'] = array("egt", $params['start_time'] .' 00:00:00');
                }else{
                    $map['c.audit_time'] = array("egt", $params['start_time'] .' 00:00:00');
                }
            } else {
                if($params['status'] == 1){
                    $map['c.posttime'] = array(array("egt", $params['start_time'] .' 00:00:00'), array("elt", $params['end_time'] .' 23:59:59'));
                }else{
                    $map['c.audit_time'] = array(array("egt", $params['start_time'] .' 00:00:00'), array("elt", $params['end_time'] .' 23:59:59'));
                }
            } 
        }
        //车辆编号
        if (!empty($params['car_no'])) {
            $map['c.car_no'] = array('eq', $params['car_no']);
        }
        //评估师id
        if (!empty($params['checker_id'])){
            $map['co.checker_id'] = array('eq',$params['checker_id']);
        }
        //要查询的列
        $cols = "c.car_id,c.car_no,c.brand_id,c.series_id,c.model_id,c.composite_state,c.accident_level,c.posttime,c.car_status,
                c.audit_time,c.remark,co.owner_id,co.checker_id,e.real_name as checker_name";
        $prefix = C('DB_PREFIX');

        $total = M('cars')->alias('c')->field($cols)
                        ->join("{$prefix}car_owner as co ON co.owner_id=c.owner_id")
                        ->join("LEFT JOIN {$prefix}employee as e On e.emp_id=co.checker_id")
                        ->where($map)->count();

        $list = M('cars')->alias('c')->field($cols)
                        ->join("{$prefix}car_owner as co ON co.owner_id=c.owner_id")
                        ->join("LEFT JOIN {$prefix}employee as e On e.emp_id=co.checker_id")
                        ->where($map)->limit($size)->page($page)->order($order)->select();
//        echo M()->getLastSql();
        if (!empty($list)) {
            $cm = new \AuctionCore\CarResource\CarModel();
            foreach ($list as $key => $val) {
                $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
                $series = $cm->get_model_by_id($val['series_id'], 'series');
                $model = $cm->get_model_by_id($val['model_id']);
                $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
            }
        }

        $result = array(
            'total' => $total,
            'list' => $list
        );
        return $result;
    }

}

?>